package com.example.commonlib.base.widget;

import com.example.commonlib.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * MaterialDialog 提示框，可设置title，content，两个按钮监听器
 * Created by linlongxin on 2016/8/3.
 */

public class MaterialDialog extends CommonDialog {

    private Text mTitle;
    private Text mContent;
    private Text mPositiveButton;
    private Text mPassiveButton;

    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MaterialDialog");

    public MaterialDialog(Context context) {
        this(context, ResourceTable.Id_iop);
        HiLog.info(LABEL, "MaterialDialog");
    }

    public MaterialDialog(Context context, int themeResId) {
        super(context);
    }

    protected MaterialDialog(Context context, boolean cancelable, RemoveCallback removeCallback) {
        super(context);
        siteRemovable(cancelable);
        registerRemoveCallback(removeCallback);
    }

    @Override
    protected void onCreate() {
        HiLog.info(LABEL, "onCreate");
        super.onCreate();
    }

    public void setTitle(CharSequence title) {
        if (!TextUtils.isEmpty(title)) {
            mTitle.setText((String) title);
        }
    }

    private void setContent(CharSequence content) {
        if (!TextUtils.isEmpty(content)) {
            mContent.setText((String) content);
            mContent.setVisibility(Component.VISIBLE);
        }
    }

    /**
     * 确认按钮监听器
     */
    public void setPositiveClickListener(final ClickedListener listener) {
        mPositiveButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (listener != null) {
                    listener.onClick(MaterialDialog.this, 0);
                }
            }
        });
    };

    /**
     * 取消安装监听器
     */
    public void setPassiveClickListener(final ClickedListener listener) {
        mPassiveButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (listener != null) {
                    listener.onClick(MaterialDialog.this, 1);
                }
            }
        });
    }

    public CharSequence getTitle() {
        return mTitle.getText();
    }

    public CharSequence getContent() {
        return mContent.getText();
    }

    private static class MaterialParam {

        private boolean cancelable = true;
        private CharSequence mTitle;
        private CharSequence mPositiveText;
        private CharSequence mPassiveText;
        private ClickedListener mPositiveListener;
        private ClickedListener mPassiveListener;
    }

    public static class Builder {

        private MaterialParam mMaterialParam;
        private Context mContext;

        public Builder(Context context) {
            mMaterialParam = new MaterialParam();
            mContext = context;
        }

        public Builder setTitle(CharSequence title) {
            mMaterialParam.mTitle = title;
            return this;
        }

        public Builder setPositiveText(CharSequence text) {
            mMaterialParam.mPositiveText = text;
            return this;
        }

        public Builder setPassiveText(CharSequence text) {
            mMaterialParam.mPassiveText = text;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            mMaterialParam.cancelable = cancelable;
            return this;
        }

        public Builder setOnPositiveClickListener(ClickedListener listener) {
            mMaterialParam.mPositiveListener = listener;
            return this;
        }

        public Builder setOnPassiveClickListener(ClickedListener listener) {
            mMaterialParam.mPassiveListener = listener;
            return this;
        }

        public MaterialDialog create() {
            MaterialDialog dialog = new MaterialDialog(mContext);
            Component dialogContent = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_dialog_material, null, false);

            dialog.mTitle = (Text) dialogContent.findComponentById(ResourceTable.Id_title);
            dialog.mContent = (Text) dialogContent.findComponentById(ResourceTable.Id_input_content);
            dialog.mPositiveButton = (Text) dialogContent.findComponentById(ResourceTable.Id_passive_button);
            dialog.mPassiveButton = (Text) dialogContent.findComponentById(ResourceTable.Id_positive_button);

            if (!TextUtils.isEmpty(mMaterialParam.mPositiveText)) {
                dialog.mPositiveButton.setText((String) mMaterialParam.mPositiveText);
            }
            if (!TextUtils.isEmpty(mMaterialParam.mPassiveText)) {
                dialog.mPassiveButton.setText((String) mMaterialParam.mPassiveText);
            }
            if (!TextUtils.isEmpty(mMaterialParam.mTitle)) {
                dialog.setTitle(mMaterialParam.mTitle);
            }
            dialog.setPositiveClickListener(mMaterialParam.mPositiveListener);
            dialog.setPassiveClickListener(mMaterialParam.mPassiveListener);
            dialog.siteRemovable(mMaterialParam.cancelable);

            dialog.setTitleText("");
            dialog.setContentCustomComponent(dialogContent);
            return dialog;
        }

        public void show() {
            create().show();
        }

    }
}
